package com.lottery.ruok.lottrey.activity;

import android.os.Bundle;
import android.view.View;
import android.view.ViewStub;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.adapter.FavorAdapter;
import com.lottery.ruok.lottrey.eventbusObject.FavorDeleted;
import com.lottery.ruok.lottrey.retrofitModule.RetrofitBase;
import com.lottery.ruok.lottrey.retrofitModule.RxHelper;
import com.lottery.ruok.lottrey.retrofitModule.RxSubscribe;
import com.lottery.ruok.lottrey.retrofitModule.request.GetFavorListRequest;
import com.lottery.ruok.lottrey.retrofitModule.request.RemoveFavorRequest;
import com.lottery.ruok.lottrey.retrofitModule.response.GetFavorListResponse;
import com.lottery.ruok.lottrey.retrofitModule.response.OperationSuccessResponse;
import com.lottery.ruok.lottrey.userInfo.UserInfo;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Iterator;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import ui.NavigateBar;

/*
* 我的收藏
* */

public class FavorActivity extends BaseActivity {
    private final static String ANALYTIC_VERSION = "1.0";
    @BindView(R.id.navigateBar)
    NavigateBar navigateBar;
    private ArrayList<GetFavorListResponse> getFavorListResponses;

    @BindView(R.id.listview)
    ListView listview;
    @BindView(R.id.all_choose)
    LinearLayout allChoose;
    @BindView(R.id.all_not_choose)
    LinearLayout allNotChoose;
    @BindView(R.id.choose)
    FrameLayout choose;
    @BindView(R.id.delete)
    Button delete;
    @BindView(R.id.bottom_buttons)
    LinearLayout bottomButtons;
    private View emptyLayout;

    private FavorAdapter myAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_favor);
        ButterKnife.bind(this);

        init();
    }

    private void init() {
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if(navigateBar.getOperateTitle().equals("取消")){
                    if( getFavorListResponses.get(i).isChoose()) {
                        getFavorListResponses.get(i).setChoose(false);
                    } else {
                        getFavorListResponses.get(i).setChoose(true);
                    }
                    myAdapter.notifyDataSetChanged();
                    if(myAdapter.isAllSelect()){
                        allChoose.setVisibility(View.GONE);
                        allNotChoose.setVisibility(View.VISIBLE);
                    } else{
                        allChoose.setVisibility(View.VISIBLE);
                        allNotChoose.setVisibility(View.GONE);
                    }
                } else {
//                    Intent intent = new Intent(FavorActivity.this, LotteryDetailActivity.class);
//                    intent.putExtra("code", getFavorListResponses.get(i).getItem_code());
//                    startActivity(intent);
                    gotoLotteryDetailActivity(getFavorListResponses.get(i).getItem_code(), getFavorListResponses.get(i).getItem_class());
                }
            }
        });

        navigateBar.setOnOperateClickListener(new NavigateBar.OnOperateClickListener() {
            @Override
            public void onOperateClick(View view) {
                if(navigateBar.getOperateTitle().equals("取消")){
                    navigateBar.setOperateTitle("删除");

                    bottomButtons.setVisibility(View.GONE);

                    if(getFavorListResponses != null){
                        for(GetFavorListResponse getFavorListResponse : getFavorListResponses){
                            getFavorListResponse.setChoose(false);
                        }
                    }

                    allChoose.setVisibility(View.VISIBLE);
                    allNotChoose.setVisibility(View.GONE);
                } else {
                    navigateBar.setOperateTitle("取消");

                    bottomButtons.setVisibility(View.VISIBLE);
                }

                if(myAdapter != null) {
                    myAdapter.notifyDataSetChanged();
                }
            }
        });

        getList();
    }

    private void setEmtpyView(){
        if (emptyLayout == null) {
            ViewStub stub = findViewById(R.id.no_net_viewstub);
            View view = stub.inflate();
            emptyLayout = view.findViewById(R.id.no_net_view);
            emptyLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    getList();
                }
            });
        }else if(emptyLayout != null && emptyLayout.getVisibility() == View.GONE){
            emptyLayout.setVisibility(View.VISIBLE);
        }
    }

    private void getList(){
        RetrofitBase.getRetrofit().create(GetFavorListRequest.class).getResult(UserInfo.getToken())
                .compose(RxHelper.<ArrayList<GetFavorListResponse>>handleResult())
                .subscribe(new RxSubscribe<ArrayList<GetFavorListResponse>>(this) {
                    @Override
                    protected void _onNext(ArrayList<GetFavorListResponse> g) {
                        getFavorListResponses = g;

                        myAdapter = new FavorAdapter(getFavorListResponses, FavorActivity.this, navigateBar);
                        listview.setAdapter(myAdapter);

                        if(getFavorListResponses.size() == 0) {
                            navigateBar. setOperateTitleGone(true);
                            setEmtpyView();
                        } else {
                            navigateBar. setOperateTitleGone(false);
                            if(emptyLayout != null){
                                emptyLayout.setVisibility(View.GONE);
                            }
                        }
                    }

                    @Override
                    protected boolean showDialog() {
                        return false;
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);

                        setEmtpyView();
                    }
                });
    }

    @OnClick({R.id.choose, R.id.delete})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.choose:
                if(allChoose.getVisibility() == View.VISIBLE){
                    allChoose.setVisibility(View.GONE);
                    allNotChoose.setVisibility(View.VISIBLE);

                    if(getFavorListResponses != null){
                        for(GetFavorListResponse getFavorListResponse : getFavorListResponses){
                            getFavorListResponse.setChoose(true);
                        }
                    }
                    if(myAdapter != null){
                        myAdapter.notifyDataSetChanged();
                    }
                } else {
                    allChoose.setVisibility(View.VISIBLE);
                    allNotChoose.setVisibility(View.GONE);

                    if(getFavorListResponses != null){
                        for(GetFavorListResponse getFavorListResponse : getFavorListResponses){
                            getFavorListResponse.setChoose(false);
                        }
                    }
                    if(myAdapter != null){
                        myAdapter.notifyDataSetChanged();
                    }
                }
                break;
            case R.id.delete:
                //sendAnalyticMsg("lottery_" + ANALYTIC_VERSION + "_me_collection_delete");

                if(getFavorListResponses != null){
                    StringBuilder deletes = null;
                    for(GetFavorListResponse getFavorListResponse : getFavorListResponses){
                        if(getFavorListResponse.isChoose()){
                            if(deletes == null){
                                deletes = new StringBuilder(getFavorListResponse.getItem_code());
                            } else {
                                deletes.append(",").append(getFavorListResponse.getItem_code());
                            }
                        }
                    }

                    if(deletes != null){
                        RetrofitBase.getRetrofit().create(RemoveFavorRequest.class).getResult(UserInfo.getToken(), deletes.toString())
                                .compose(RxHelper.<OperationSuccessResponse>handleResult())
                                .subscribe(new RxSubscribe<OperationSuccessResponse>(this) {
                                    @Override
                                    protected void _onNext(OperationSuccessResponse operationSuccessResponse) {
                                        //删除成功
                                        Iterator<GetFavorListResponse> sListIterator = getFavorListResponses.iterator();
                                        while(sListIterator.hasNext()){
                                            GetFavorListResponse getFavorListResponse = sListIterator.next();
                                            if(getFavorListResponse.isChoose()){
                                                sListIterator.remove();
                                            }
                                        }
                                        if (getFavorListResponses.size()==0){
                                            navigateBar.setOperateTitleGone(true);
                                            setEmtpyView();
                                        }else {
                                            navigateBar.setOperateTitleGone(false);
                                        }
                                        myAdapter.notifyDataSetChanged();

                                        navigateBar.setOperateTitle("删除");

                                        bottomButtons.setVisibility(View.GONE);

                                        allChoose.setVisibility(View.VISIBLE);
                                        allNotChoose.setVisibility(View.GONE);
                                        EventBus.getDefault().post(new FavorDeleted());
                                    }

                                    @Override
                                    protected boolean showDialog() {
                                        return true;
                                    }
                                });
                    }
                }
                break;
        }
    }
}
